//
//  TJTemplateChildController.m
//  BaoDian
//
//  Created by mac on 2024/7/24.
//

#import "TJTemplateChildController.h"
#import "TJTemplateCell.h"
#import "TJTemplateDetailController.h"
#import "TJTemplateMultipleDetailController.h"

@interface TJTemplateChildController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) NSMutableArray *datas;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;


@end

@implementation TJTemplateChildController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = UIColor.tj_backgroundColor;
    
    [self.view addSubview:self.collectionView];

    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(2);
        make.bottom.offset(0);
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TJTemplateCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TJTemplateCell" forIndexPath:indexPath];
    cell.model = self.datas[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    TJTemplateModel *model = self.datas[indexPath.row];
    
    NSLog(@"model=%@",model);
    
    if (model.type.intValue == 1) {
        TJTemplateMultipleDetailController *templateDetailController = [[TJTemplateMultipleDetailController alloc] init];
        templateDetailController.model = model;
        [[TJTools currentNavController] pushViewController:templateDetailController];
        return;
    }
    
    TJTemplateDetailController *templateDetailController = [[TJTemplateDetailController alloc] init];
    templateDetailController.model = model;
    [[TJTools currentNavController] pushViewController:templateDetailController];

}
 
- (void)loadData {
    if (self.datas.count > 0) {
        return;
    }
    
    [TJTemplatePresent loadIndustryDataWithId:self.type pageIndex:1 complete:^(NSArray * _Nonnull datas, NSError * _Nullable error) {
        [self.datas removeAllObjects];
        if (datas) {
            [self.datas addObjectsFromArray:datas];
        }
        [self.collectionView reloadData];
    }];
}
 

-(UICollectionView *)collectionView {
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumLineSpacing = 12;
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 16, 12, 16);
        flowLayout.itemSize = CGSizeMake(ceilf((KScreenWidth - 32 - 12) / 2), HZoom(206));
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _flowLayout = flowLayout;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor tj_backgroundColor];
        [_collectionView registerClass:TJTemplateCell.class forCellWithReuseIdentifier:@"TJTemplateCell"];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
         _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.alwaysBounceVertical=YES;
    }
    return _collectionView;
}
- (BOOL)hiddenNavigationBar {
    return YES;
}

- (NSMutableArray *)datas {
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

@end
